from PyQt5.QtCore import Qt, QEvent, QModelIndex
from PyQt5.QtWidgets import QItemDelegate, QCheckBox


class TrueFalse(QItemDelegate):
    def __init__(self, parent, constraints):
        super().__init__(parent)
        self.constraints = constraints


    def createEditor(self, parent, option, index):
        check = QCheckBox(parent)
        return check


    def paint(self, painter, option, index):
        if not(index.data()==''):
            if(int(index.data())==0):
                check = Qt.Unchecked
            else:
                check = Qt.Checked
        else:
            check = Qt.Unchecked
        self.drawCheck(painter, option, option.rect, check)


    def editorEvent(self, event, model, option, index):
        if index.flags()=='':
            return False
        
        if not int(index.flags() & Qt.ItemIsEditable) > 0:
            return False

        if event.type() == QEvent.MouseButtonRelease and event.button() == Qt.LeftButton:
            self.setModelData(None, model, index)
            return True

        return False

    def setEditorData(self, check, index):
        if(int(index.model().data(index, Qt.EditRole))==1):
            print('Checked')
            check.setCheckState(Qt.Checked)
        else:
            print('Not Checked')
            check.setCheckState(Qt.Unchecked)


    def setModelData(self, check, model, index):
        model.setData(index, 1 if int(index.data())==0 else 0, Qt.EditRole)


    def updateEditorGeometry(self, check, option, index):
        check.setGeometry(option.rect)
